import boto3
import os
import requests
from config import *
from base_class.inspect_logging import print_to_log
from base_class.base_utils import spend_time
import json
import re
import sys
import datetime


class DataStep():

    def __init__(self, s3_path, url, local_path, buc):
        self.filename = None
        self.full_filename = None
        self.url = url
        self.path = local_path
        self.s3 = boto3.resource('s3')
        self.bucket = buc
        self.s3_path = s3_path

    @spend_time("下载文件耗时：")
    def download(self, dateparameters):

        global r
        self.filename = f"{dateparameters}.json"
        self.url += dateparameters

        if not os.path.exists(self.path):
            os.makedirs(self.path)

        # 下载文件
        try:
            print_to_log('Downloading...')
            print_to_log("url",self.url)
            r = requests.get(self.url)
        except Exception as e:
            print_to_log(e)
            print_to_log(RESQUESTFAILED)

        try:
            self.full_filename = self.path + self.filename
            data = r.json()["list"]
            if data:
                with open(self.full_filename, "w", encoding='utf-8') as outFile:
                    for i in data:
                        b = json.dumps(i, ensure_ascii=False)
                        outFile.write(b + ',\n')
                return True
            else:
                print_to_log("还没数据。。。。")
                print_to_log(SUCCESS)
                return False
        except Exception as e:
            print_to_log(e)
            print_to_log(PROGRAMERROR)

    @spend_time("上传文件耗时：")
    def upload(self):
        try:
            self.s3.Object(self.bucket, self.s3_path + self.filename).upload_file(self.full_filename)
            os.remove(self.full_filename)
            print_to_log(SUCCESS)
        except Exception as e:
            print_to_log(e)
            print_to_log(PROGRAMERROR)


